lobsterlayui.main(['index', 'layer', 'form', 'element', 'table', 'tree'], {
    data: {
        $layui: null,
        tableins: 0,//表格
        list: [],//列表
    },
    pageload: function (right) {
        console.log(right);
        var self = this;
        self.pageEvent();
        self.listLoad();
    },
    //加载列表数据
    listLoad: function () {
        var self = this;
        self.data.tableins = layui.table.render({
            elem: "#deptTable",
            headers: lobsterlayui.GetHeaderData(),
            where: {
                deptName: $("#DeptName").val(),
                delFlag: $("#delFlag").val()
            },
            url: lobsterlayui.GetLowCodeUrl('tsAdmin.getDeptList'),
            height: 'full-90',
            size: '10',
            toolbar: '#deptadd',
            page: true, //开启分页 
            cols: [[
                { field: 'Code', title: '科室编号', width: '120' },
                { field: 'Name', title: '科室', width: '200' },
                { field: 'DelFlag', title: '状态', align: 'center', templet: "#status", width: '100' },
                { field: 'WorkDeptId', title: '机构科室编号', width: '120' },
                { field: 'WorkDeptName', title: '机构科室', width: '200' },
                {
                    title: '操作', width: '300', templet: function (d) {
                        var appendhtml = ' <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>';
                        if (d.DelFlag == 0)
                            appendhtml += '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="enstatus"><i class="layui-icon layui-extend-stop"></i>停用</a>';
                        else
                            appendhtml += '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="enstatus"><i class="layui-icon layui-icon-ok"></i>启用</a>';
                        appendhtml += '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="relation"><i class="layui-icon layui-icon-edit">关联科室</i></a>';
                        return appendhtml;

                    }
                }
            ]]
            , done: res => {
            },
            parseData: function (res) {
                return {
                    code: res.code,
                    msg: res.msg,
                    count: res.data.count,
                    data: res.data.list
                }
            }
        });
    },
    //添加事件
    pageEvent: function () {
        var self = this;
        //查询
        layui.form.on("submit(query-submit)", function (obj) {
            self.listLoad();
        });
        // 监听表头工具栏
        layui.table.on("toolbar(deptTable)", function (obj) {
            if (obj.event == "add") {
                self.viewEdit(null);
            }
        });
        //监听按钮事件
        layui.table.on('tool(deptTable)', function (obj) {
            var data = obj.data;//当前行数据
            var layEvent = obj.event;//获得lay-event的值
            switch (layEvent) {
                case 'edit':
                    self.viewEdit(data);
                    break;
                case 'enstatus':
                    data.DelFlag = data.DelFlag == "0" ? 1 : 0;
                    self.updateState(data);
                    break;
                case 'relation':
                    window.data = {
                        selectdata: data,
                        branchList: self.data.branchList,
                        hisdeptList: self.data.hisdeptList
                    }
                    self.viewRelation();
                    break;
            }
        });
    },
    //弹出编辑框
    viewRelation: function () {
        var self = this;
        layer.msg('未实现')
    },
    //弹出编辑框
    viewEdit: function (data) {
        var self = this;
        if (!data) {
            data = { DeptId: 0, Name: "", Pym: "", Code: 0, DelFlag: 0, SortOrder: 1 };
        }

        layer.open({
            type: 1
            , title: '科室编辑'
            , btn: ['保存', "关闭"]
            , content: $('#openDept').html()
            , area: ['500px', '50%']  //宽，高
            , cancel: function (index, layero) {
                layer.close(index);
            }
            , success: function () {
                $("#deptName").val(data.Name);
            }
            , yes: function (index, layero) {
                data.Name = $("#deptName").val();
                self.save(data, () => {
                    layer.close(index);
                })
            }
        });
    },
    /**
     * 保存
     * */
    save: function (data, success) {
        if (!data.Name) {
            layer.msg("请完善信息");
            return;
        }
        console.log(data);
        var self = this;
        lobsterlayui.POSTAPI('tsAdmin.saveDept', data).then(retdata => {
            layer.msg("操作成功");
            self.listLoad();
            success && success();
        }).catch(res => {
            layer.msg(res.msg);
        })
    },
    /**
    * 修改状态
    * */
    updateState: function (data) {
        var self = this;
        lobsterlayui.GETAPI('tsAdmin.updateDeptState', { deptId: data.DeptId, status: data.DelFlag }).then(retdata => {
            layer.msg("操作成功");
            self.listLoad();
        })
    },
})